package org.reyton.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.reyton.dao.BizClaimVoucherDao;
import org.reyton.entity.BizClaimVoucher;
import org.reyton.entity.SysEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 报销单明细表Dao实现类
 * Created by reyton on 16-12-1.
 */
@Repository("bizClaimVoucherDao")
public class BizClaimVoucherDaoImpl extends HibernateDaoSupport implements BizClaimVoucherDao {
    /***
     * 按名称自动装配
     * @param sessionFactory
     */
    @Autowired
    @Qualifier("sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public void save(BizClaimVoucher bizClaimVoucher) {
        this.getHibernateTemplate().save(bizClaimVoucher);
    }

    @Override
    public void updateAccount(BizClaimVoucher bizClaimVoucher) {
        final String sql = "update BIZ_CLAIM_VOUCHER set TOTAL_ACCOUNT = ? where ID = ?";
        this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                int i = 0;
                session.createSQLQuery(sql).
                        setInteger(i, bizClaimVoucher.getTotalAccount()).
                        setInteger(1, bizClaimVoucher.getId()).executeUpdate();
                return null;
            }
        });
    }

    @Override
    public void updateDes(BizClaimVoucher bizClaimVoucher) {
        final String sql = "update BIZ_CLAIM_VOUCHER set EVENT = ? where ID = ? ";
        this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                int i = 0;
                session.createSQLQuery(sql).
                        setString(i, bizClaimVoucher.getEvent()).
                        setInteger(1, bizClaimVoucher.getId()).executeUpdate();
                return null;
            }
        });
    }

    @Override
    public void updateStatus(BizClaimVoucher bizClaimVoucher) {
        final String sql = "update BIZ_CLAIM_VOUCHER set STATUS = ? where ID = ? ";
        this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                int i = 0;
                session.createSQLQuery(sql).
                        setInteger(i, bizClaimVoucher.getSysDictionary().getId()).
                        setInteger(1, bizClaimVoucher.getId()).executeUpdate();
                return null;
            }
        });
    }

    @Override
    public List<BizClaimVoucher> findPageById(SysEmployee sysEmployee, int pageSize, int pageIndex) {
        final String hql = "from BizClaimVoucher where sysEmployeeByCreateSn = ?";
        List<BizClaimVoucher> b = this.getHibernateTemplate().execute(new HibernateCallback<List<BizClaimVoucher>>() {
            @Override
            public List<BizClaimVoucher> doInHibernate(Session session) throws HibernateException {
                return (List<BizClaimVoucher>) session
                        .createQuery(hql)
                        .setString(0, sysEmployee.getSn())
                        .setFirstResult((pageIndex - 1) * pageSize)
                        .setMaxResults(pageSize)
                        .list();
            }
        });
        return b;
    }

    @Override
    public long findCountById(SysEmployee sysEmployee) {
        final String hql = "select count(b.id) from BizClaimVoucher b where sysEmployeeByCreateSn.sn=?";
        Long i = this.getHibernateTemplate().execute(new HibernateCallback<Long>() {
            @Override
            public Long doInHibernate(Session session) throws HibernateException {
                return (long) session.createQuery(hql).setString(0, sysEmployee.getSn()).uniqueResult();
            }
        });
        return i;
    }

    @Override
    public void delete(BizClaimVoucher bizClaimVoucher) {
        this.getHibernateTemplate().delete(bizClaimVoucher);
    }

    @Override
    public BizClaimVoucher findById(int id) {
        return  (BizClaimVoucher)this.getHibernateTemplate().find("from BizClaimVoucher where id = ?",id).get(0);
    }

    @Override
    public List<BizClaimVoucher> findByNextDealSn(String NextDealSn,int pageSize,int pageIndex) {
        final String hql = "from BizClaimVoucher where sysEmployeeByNextDealSn = ? " +
                            "order by sysDictionary.id asc ,createTime desc ";
        List<BizClaimVoucher> b = this.getHibernateTemplate().execute(new HibernateCallback<List<BizClaimVoucher>>() {
            @Override
            public List<BizClaimVoucher> doInHibernate(Session session) throws HibernateException {
                return (List<BizClaimVoucher>) session
                        .createQuery(hql)
                        .setString(0, NextDealSn)
                        .setFirstResult((pageIndex - 1) * pageSize)
                        .setMaxResults(pageSize)
                        .list();
            }
        });
        return b;
    }

}
